//获取应用实例
var util = require('../../util.js');
var app = getApp();

// 引入SDK核心类
var QQMapWX = require('../../qqmap-wx-jssdk/qqmap-wx-jssdk');
// 实例化API核心类
var demo = new QQMapWX({
  key: 'UQ3BZ-5PM3F-PUTJB-J746B-5YA5Z-YOBC5' // 必填
});
// 初始化数据
var init = {

  // 数据
  data: {

    // 数据加载
    loading: false,
    showall:false,
    Height: 0,
    scale: 13,
    latitude: "",
    longitude: "",
    markers: [],
    circles: [],
    total:0

  },
  
  // 页面第一次加载
  onLoad: function (options) {
    this.setData({
      options: options
    })
    var that = this;

    wx.getSystemInfo({
      success: function (res) {
        //设置map高度，根据当前设备宽高满屏显示
        that.setData({
          view: {
            Height: res.windowHeight
          }
        })
      }
    })
    wx.getLocation({
      type: 'wgs84', // 默认为 wgs84 返回 gps 坐标，gcj02 返回可用于 wx.openLocation 的坐标
      success: function (res) {

        that.setData({
          latitude: res.latitude + 0.01,
          longitude: res.longitude,
          markers: [{
            id: "1",
            latitude: res.latitude,
            longitude: res.longitude,
            width: 35,
            height: 35,
            borderRadius: 100,
            iconPath: "",
            anchor: { x: .5, y: 0.8 }

          }],
          // circles: [{
          //   latitude: res.latitude,
          //   longitude: res.longitude,
          //   color: '#FF0000DD',
          //   fillColor: '#7cb5ec88',
          //   radius: 3000,
          //   strokeWidth: 1
          // }]//范围圆

        })
        console.log(that.data.latitude);
      }

    })
  },
  regionchange(e) {
    console.log("regionchange===" + e.type)
  },
  // 每次加载都调用
  onShow: function () {
    this.getData();
  },

  // 获取数据
  getData: function () {

    var that = this;
    app.getLoginSession(function (session) {
      wx.request({

        // 请求地址
        url: app.api,

        // 请求参数
        data: {
          app: 'buyer_order',
          act: 'view',
          order_id:that.data.options.order_id,
          order_sn:that.data.options.order_sn,
          t_session: session
        },

        // 请求成功
        success: function (res) {
          console.log(res.data);
          var data = res.data;

          if (data.done && data.retval) { // 成功

            var status = '';
            if (data.retval.status == 10) {
              status = '待付款'
            } else if (data.retval.status == 20) {
              status = '待接单'
            } else if (data.retval.status == 30) {
              status = '订单进行中'
            } else if (data.retval.status == 40) {
              status = '待评价'
            } else if (data.retval.status == 41) {
              status = '退款中'
            } else if (data.retval.status == 42) {
              status = '商家正在处理'
            } else if (data.retval.status == 43) {
              status = '退款成功'
            } else if (data.retval.status == 0) {
              status = '交易关闭'
            }

            that.setData({

              // 关闭loading效果
              loading: false,

              // 地址列表
              data: data.retval,
              total: that.deallist(data.retval),
              // 订单状态
              status: status,
              foodlist:data.retval.foods

            });
            that.data.data.add_time = util.formatDate(new Date(that.data.data.add_time * 1000), 'YY-MM-dd');
            that.data.data.total_money = parseFloat(that.data.data.total_money),
            that.data.data.distribution_fee = parseFloat(that.data.data.distribution_fee),
            that.setData({
              data: that.data.data,
              cutmoney: parseFloat(that.data.total - that.data.data.total_money +that.data.data.distribution_fee).toFixed(2),
              cutmoneys: parseFloat(that.data.total - that.data.data.total_money).toFixed(2)
            })
            console.log(that.data.total);
            that.setData({
              latitude: parseInt(that.data.data.latitude) + 0.01,
              longitude: parseInt(that.data.data.longitude),
              markers: [{
                id: "1",
                latitude: parseInt(that.data.data.latitude),
                longitude: parseInt(that.data.data.longitude),
                width: 35,
                height: 35,
                borderRadius: 100,
                iconPath: that.data.data.store_banner,
                anchor: { x: .5, y: 0.8 }
              }],
              // circles: [{
              //   latitude: res.latitude,
              //   longitude: res.longitude,
              //   color: '#FF0000DD',
              //   fillColor: '#7cb5ec88',
              //   radius: 3000,
              //   strokeWidth: 1
              // }]//范围圆

            })
            console.log(that.data.latitude);
          } else { // 失败
            that.setData({
              errorTips: data.msg || '请求错误，act：index'
            })
          }

        },

        // 地址列表
        fail: function () {
          that.setData({
            errorTips: '接口请求失败，act：index'
          })
        }
      })
    })

  },

  // 拨打客服电话
  phoneCall: function () {
    wx.makePhoneCall({
      phoneNumber: this.data.data.store_phone
    })
  },
  // 显示所有菜单
  show_all:function(){
    var that = this;
    that.setData({
      showall:that.data.showall==true?false:true
    })
  },
  //付款
  paybuy:function(event){
    console.log(event.currentTarget.dataset.amount);
    app.callToPay(event.currentTarget.dataset.orderid, event.currentTarget.dataset.amount);
  },
  //取消订单
  cancel: function (res) {
    var that = this;
    app.getLoginSession(function (session) {
      wx.request({
        url: app.api,
        data: {
          app: 'order',
          act: 'cancel_order',
          order_id: res.currentTarget.dataset.orderid,
          t_session: session
        },
        success: function (res) {
          var data = res.data;
          if (data.done && data.retval) {
            that.getData();
          } else {
            wx.showToast({
              title: '请重试',
              icon: 'loading'
            })
          }
        },
        fail: function (res) {
          wx.showToast({
            title: '接口连接失败',
            icon: 'loading'
          })
        }
      })
    })

  },
  deallist(list) {
    var that =this;
    var arr = list.foods;
    if (arr && Array.isArray(arr)) {
      arr.forEach((item) => {
        that.setData({
          total: that.data.total + parseFloat(item.discount_price)*item.quantity
        })
      })
      return that.data.total
    }
  },

};

// 注册页面
Page(init);
